package test_junit_shop;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import util.BaseDriver;

import java.util.List;


/**
 * Created by Eric on 2018/2/5.
 * 门店-购物单管理
 */
public class ShoppingList extends BaseDriver {
    private static ShoppingList sl = new ShoppingList();
    public static ShoppingList getInstance(){
        return sl;
    }

    /**
     * 配货
     * @param customerName 客户名
     */
    public void allocateGoods(String customerName) throws InterruptedException {
        Thread.sleep(3000);
        driver.switchTo().defaultContent();
        List<WebElement> frameList = driver.findElements(By.xpath("//iframe[@name='iframeundefined']"));
        driver.switchTo().frame(frameList.get(frameList.size() - 1));

        driver.findElement(By.xpath(String.format("//td[contains(text(), '%s')]/following-sibling::td[8]/select/option[2]", customerName))).click();

        Thread.sleep(1000);
        driver.switchTo().frame(0);
        driver.findElement(By.cssSelector("span.stockTex")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("//td[contains(text(), '门店创建仓库')]")).click();
        Thread.sleep(500);

        driver.switchTo().parentFrame();
        driver.findElement(By.linkText("确定出库")).click();

        Thread.sleep(2000);
        driver.findElement(By.linkText("确定")).click();
        Thread.sleep(2000);
    }

    /**
     * 退货
     * @param customerName 客户名
     */
    public void refund(String customerName) throws InterruptedException {
        Thread.sleep(5000);
        driver.switchTo().defaultContent();
        List<WebElement> frameList = driver.findElements(By.xpath("//iframe[@name='iframeundefined']"));
        driver.switchTo().frame(frameList.get(frameList.size() - 1));

        driver.findElement(By.xpath(String.format("//td[contains(text(), '%s')]/following-sibling::td[8]/select/option[4]", customerName))).click();
        Thread.sleep(2000);

        driver.switchTo().frame(0);
        driver.findElement(By.xpath("//td[@class='bs-checkbox']")).click();

        WebElement aliPayAccount = driver.findElement(By.name("payAccountNo"));
        new Actions(driver).moveToElement(aliPayAccount);
        aliPayAccount.sendKeys("123456789");

        driver.switchTo().parentFrame();
        driver.findElement(By.linkText("确定")).click();
        Thread.sleep(1000);

        driver.findElements(By.linkText("确定")).get(1).click();
        Thread.sleep(1000);
    }
}
